#pragma once

#include "ilogger.hpp"
#include "metrics/metrics_collector.hpp"
#include <string>

class MetricsLoggerDecorator : public ILogger {
public:
    // 包装目标Logger 和可选名称(用于区分多个Logger实例)
    MetricsLoggerDecorator(std::shared_ptr<ILogger> target, std::string name = "default")
        : m_target(std::move(target))
        , m_name(std::move(name)) {}

    // 所有ILogger接口转发给目标Logger， 并添加Metrics逻辑
    void setLevel(LogLevel level) override { m_target->setLevel(level); }
    bool shouldLog(LogLevel level) const override { return m_target->shouldLog(level); }

    void log(LogLevel  level, const std::string& message, const nlohmann::json& context) override {
        get_metrics_collector()->inc_total_logs();
        m_target->log(level, message, context);
    }

    void trace(const std::string& message, const nlohmann::json& context) override {
        get_metrics_collector()->inc_total_logs();
        m_target->trace(message, context);
    }

    void debug(const std::string& message, const nlohmann::json& context) override {
        get_metrics_collector()->inc_total_logs();
        m_target->debug(message, context);
    }

    void info(const std::string& message, const nlohmann::json& context) override {
        get_metrics_collector()->inc_total_logs();
        m_target->info(message, context);
    } 

    void warn(const std::string& message, const nlohmann::json& context) override {
        get_metrics_collector()->inc_total_logs();
        m_target->warn(message, context);
    }

    void error(const std::string& message, const nlohmann::json& context) override {
        get_metrics_collector()->inc_total_logs();
        m_target->error(message, context);
    }

    void critical(const std::string& message, const nlohmann::json& context) override {
        get_metrics_collector()->inc_total_logs();
        m_target->critical(message, context);
    }

    // 若目标Logger支持记录丢弃日志（需要接口扩展）
    void on_log_dropped() {
        get_metrics_collector()->inc_dropped_logs();
    }

    void flush() override {
        m_target->flush();
    }

    void dumpBacktrace() override {
        m_target->dumpBacktrace();
    }

    void enableBacktrace(size_t size) override {
        m_target->enableBacktrace(size);
    }


    std::string getFormatterType() override {
        return m_target->getFormatterType();
    } 

private:
    std::shared_ptr<ILogger> m_target;   // 被包装的实际Logger  
    std::string m_name;                  // Logger实例名称
};
